package com.itheima.exerciseTest.method20_29.method25;

public class MyThread extends Thread {
    private Resource res;

    public MyThread(Resource res){
        this.res = res;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while(true){
            //上锁
            synchronized (res.getLock()){
                if(res.getTickets() <= 0)
                    break;
                System.out.print(Thread.currentThread().getName() + "正在买票.......");
                res.setTickets(res.getTickets() - 1);
                System.out.println("还剩" + res.getTickets() + "张票");
            }
        }
    }
}
